﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GeckoPhotos.Tests.Support
{
    
    public static class CSVUtil
    {

        public static string BytesToCSV(byte[] bytes)
        {
            StringBuilder sb = new StringBuilder();
            foreach (byte b in bytes)
            {
                if (sb.Length != 0)
                    sb.Append(", ");
                sb.Append(b);
            }
            return sb.ToString();
        }

        /// <summary>
        /// Reads the entire stream (regardless of current position) and returns its 
        /// bytes as a CSV string.
        /// </summary>
        /// <param name="memoryStream"></param>
        /// <returns></returns>
        public static string MemoryStreamToByteCSV(System.IO.MemoryStream memoryStream)
        {
            StringBuilder sb = new StringBuilder();
            memoryStream.Position = 0;
            using (var br = new System.IO.BinaryReader(memoryStream, Encoding.UTF8))
            {
                for (int p = 0; p < memoryStream.Length; p++)
                {
                    if (sb.Length > 0)
                        sb.Append(", ");

                    byte b = br.ReadByte();
                    sb.Append(b);
                }
            }

            return sb.ToString();
        }
    }

}
